package com.ruike.alisurface.http;

import com.ruike.alisurface.bean.CarGoodsBeans;
import com.ruike.alisurface.bean.CreateOrderByProductBean;
import com.ruike.alisurface.bean.CreateOrderBySlotBean;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.ApiTools;
import com.voodoo.lib_frame.httpTools.RequestParams;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.ShareUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 天虹积分接口
 *
 * @author voodoo
 * @createDate 2023/03/06 006上午 11:32
 */
public class OtherHttp {

    // 获取商品列表
    public static final String GET_SLOTS_FOR_POINT_URL = "http://www.shouhuojiyun.com/Rknew/mchapi/getSlots_for_point";
    // 按照货道下单
    public static final String CREATE_PAY_ORDER_URL = "https://rk.shouhuojiyun.com/otherapi/tianhong/create_pay_order";
    // 按照商品类型下单
    public static final String CREATE_PAY_ORDER_BY_PRODUCT_URL = "https://rk.shouhuojiyun.com/otherapi/tianhong/create_pay_order_by_product";
    // 轮询支付结果
    public static final String SEARCH_ORDER_URL = "https://rk.shouhuojiyun.com/otherApi/tianhong/search_order";
    // 取消订单
    public static final String CANCEL_ORDER_URL = "https://rk.shouhuojiyun.com/otherapi/tianhong/close_order";
    // 订单完结接口
    public static final String END_ORDER_URL = "https://rk.shouhuojiyun.com/otherapi/tianhong/end_order";

    /**
     * 获取商品列表数据
     *
     * @param apiListener 回调
     */
    public static void getSlots(BaseView apiListener) {
        RequestParams params = new RequestParams();
        params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        new ApiTools().getRequest(GET_SLOTS_FOR_POINT_URL, params, apiListener);
    }

    public static void createOrderBySlot(List<CarGoodsBeans> carGoodsList, BaseView apiListener) {
        StringBuilder slotIndexsSb = new StringBuilder();
        for (CarGoodsBeans carGoodsBean : carGoodsList) {
            slotIndexsSb.append(carGoodsBean.getSlotIndexs());
            slotIndexsSb.append(",");
        }
        String slotIndexsStr = slotIndexsSb.toString().substring(0, slotIndexsSb.length() - 1);
        String[] slotSplit = slotIndexsStr.split(",");
        HashMap<String, Integer> slotNumberMap = new HashMap<>();
        for (String slotStr : slotSplit) {
            if (slotNumberMap.containsKey(slotStr)) {
                Integer integer = slotNumberMap.get(slotStr);
                slotNumberMap.put(slotStr, integer == null ? 1 : integer + 1);
            } else {
                slotNumberMap.put(slotStr, 1);
            }
        }

        List<CreateOrderBySlotBean.DetailsBean> detailsBeans = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : slotNumberMap.entrySet()) {
            CreateOrderBySlotBean.DetailsBean detailsBean = new CreateOrderBySlotBean.DetailsBean();
            detailsBean.setSlotIndex(entry.getKey());
            detailsBean.setQuantity(entry.getValue());
            detailsBeans.add(detailsBean);
        }
        CreateOrderBySlotBean createOrderBySlotBean = new CreateOrderBySlotBean();
        createOrderBySlotBean.setIsFace(false);
        createOrderBySlotBean.setMchid(ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        createOrderBySlotBean.setDetails(detailsBeans);

        new ApiTools().postJsonRequest(CREATE_PAY_ORDER_URL, GsonUtils.model2Json(createOrderBySlotBean), apiListener);
    }

    public static void createOrderByProduct(List<CarGoodsBeans> carGoodsList, BaseView apiListener) {
        StringBuilder productIdsSb = new StringBuilder();
        for (CarGoodsBeans carGoodsBean : carGoodsList) {
            productIdsSb.append(carGoodsBean.getProductid());
            productIdsSb.append(",");
        }
        String slotIndexsStr = productIdsSb.toString().substring(0, productIdsSb.length() - 1);
        String[] productIdSplit = slotIndexsStr.split(",");
        HashMap<String, Integer> productIdMap = new HashMap<>();
        for (String productIdStr : productIdSplit) {
            if (productIdMap.containsKey(productIdStr)) {
                Integer integer = productIdMap.get(productIdStr);
                productIdMap.put(productIdStr, integer == null ? 1 : integer + 1);
            } else {
                productIdMap.put(productIdStr, 1);
            }
        }

        List<CreateOrderByProductBean.DetailsBean> detailsBeans = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : productIdMap.entrySet()) {
            CreateOrderByProductBean.DetailsBean detailsBean = new CreateOrderByProductBean.DetailsBean();
            detailsBean.setProductid(entry.getKey());
            detailsBean.setQuantity(entry.getValue());
            detailsBeans.add(detailsBean);
        }

        for (CreateOrderByProductBean.DetailsBean detailsBean : detailsBeans) {
            String productId = detailsBean.getProductid();
            for (CarGoodsBeans carGoodsBeans : carGoodsList) {
                if (productId.equals(carGoodsBeans.getProductid())) {
                    List<Integer> slotIndexs = GsonUtils.jsonArray2ModelList(carGoodsBeans.getSlotIndexs(), Integer.class);
                    detailsBean.setSlotIndexs(slotIndexs);
                    break;
                }
            }
        }

        CreateOrderByProductBean createOrderByProductBean = new CreateOrderByProductBean();
        createOrderByProductBean.setMchid(ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        createOrderByProductBean.setDetails(detailsBeans);

        new ApiTools().postJsonRequest(CREATE_PAY_ORDER_BY_PRODUCT_URL, GsonUtils.model2Json(createOrderByProductBean), apiListener);
    }

    public static void searchOrder(BaseView apiListener, String orderId){
        RequestParams params = new RequestParams();
        params.put("orderId", orderId);
        new ApiTools().getRequest(SEARCH_ORDER_URL, params, apiListener);
    }

    public static void cancelOrder(String orderId, BaseView apiListener) {
        RequestParams params = new RequestParams();
        params.put("orderid", orderId);
        new ApiTools().getRequest(CANCEL_ORDER_URL, params, apiListener);
    }

    public static void endOrder(String orderId, boolean isEnd, String remark, BaseView apiListener) {
        RequestParams params = new RequestParams();
        params.put("order_id", orderId);
        params.put("is_end", isEnd);
        params.put("remark", remark);
        new ApiTools().getRequest(END_ORDER_URL, params, apiListener);
    }

//    /**
//     * 按照货道下单
//     *
//     * @param apiListener 回调监听
//     */
//    public static void createPayOrder(HashMap<String, Object> map, BaseView apiListener) {
//        RequestParams params = new RequestParams();
//        params.put(map);
//        new ApiTools().postRequest(CREATE_PAY_ORDER_URL, params, apiListener);
//    }
//
//    /**
//     * 按照商品下单
//     *
//     * @param apiListener 回调监听
//     */
//    public static void createPayOrderByProduct(HashMap<String, Object> map, BaseView apiListener) {
//        RequestParams params = new RequestParams();
//        params.put(map);
//        new ApiTools().postRequest(CREATE_PAY_ORDER_BY_PRODUCT_URL, params, apiListener);
//    }

}
